// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-04-01 20:44
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.BinaryAlgorithms;

import org.junit.jupiter.api.Test;

import java.util.Scanner;

public class toHex {
    final char[] digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public String toHex(int num) {
        int mag = Integer.SIZE - Integer.numberOfLeadingZeros(num);
        int cnt = Math.max((mag + 3) / 4, 1);
        char[] ans = new char[cnt];
        do {
            ans[--cnt] = digits[num & 15];
            num >>>= 4;
        }
        while (num != 0 && cnt > 0);
        return new String(ans);
    }


    public String toE(int num) {
        int mag = Integer.SIZE - Integer.numberOfLeadingZeros(num);
        int cnt = Math.max((mag + 2) / 3, 1);
        char[] ans = new char[cnt];
        do {
            ans[--cnt] = digits[num & 7];
            num >>>= 3;
        } while (cnt > 0 && num > 0);
        return new String(ans);
    }

    @Test
    public void shout() {
        System.out.println(toE(10));
    }


}
